home *** CD-ROM | disk | FTP | other *** search
- Path: mail2news.demon.co.uk!genesis.demon.co.uk
- From: Lawrence Kirby <fred@genesis.demon.co.uk>
- Newsgroups: comp.infosystems.www.servers.unix,comp.lang.c
- Subject: Re: Opening a binary file.... (dealing with content-type & WWW)
- Date: Thu, 18 Apr 96 20:25:54 GMT
- Organization: none
- Message-ID: <829859154snz@genesis.demon.co.uk>
- References: <317351DE.E7E@draper.com>
- Reply-To: fred@genesis.demon.co.uk
- X-NNTP-Posting-Host: genesis.demon.co.uk
- X-Newsreader: Demon Internet Simple News v1.27
- X-Mail2News-Path: genesis.demon.co.uk
-
- In article <317351DE.E7E@draper.com>
- bshalton@draper.com "Brandon Shalton" writes:
-
- >I have decided to take out the fork/exec and replace it with a
- >simple open file, read contents, send to browser routine.
- >
- >This is my first cut...
- >
- >
- >
- > fd2=open(entries[0].val,O_RDONLY);
- > for(;;)
- > {
- > if( read(fd2, tbuf, 1) <=0 ) exit(1);
- > printf("%s", tbuf);
- > }
-
- What is tbuf?
-
- It is best to talk about things like open() and read() in
- a newsgroup specific to your system since they are not part
- of the C language.
-
- Assuming that tbuf is an array of char then the read reads a single char
- into it (very inefficient) then you try to print the contents of the buffer
- with printf. The %s conversion specifier requires a pointer to a string as
- the corresponding argument. You haven't created a string (a null character
- terminated sequence of characters) here so subject to code elsewhere this
- is illegal.
-
- >This snippet does read the file, and sends the file out..but when
- >viewed from Microsoft Word, the file comes through as text, showing
- >the control characters at the header of the file. It looks like
- >ascii is being sent out rather than binary.
- >
- >I have also tried:
- >
- >
- >
- >
- > fin2=fopen(entries[0].val,
- >"rb");
- > while( ch=fgetc(fin2) != EOF )
- > printf("%c", ch);
- > exit(1);
-
- That's better and just uses standard C library functions but there are a
- couple of problems with it. Firstly != binds more tightly than = so the
- while line is equivalent to:
-
- > while( ch = (fgetc(fin2) != EOF) )
-
- whereas what you want is:
-
- > while((ch = fgetc(fin2)) != EOF)
-
- You can also use getc() instead of fgetc() as it is likely to be the more
- efficient of the two.
-
- printf("%c", ch);
-
- is a long-winded way of writing:
-
- putchar(ch);
-
- Always show your variable declarations - in this case make sure that ch is
- an int so that it can deal with EOF correctly.
-
- By default stdin,stdout and stderr are opened in text mode. Standard C
- provides no method to change this although your compiler may provide a
- system-specific method. On Unix systems there is no difference between
- binary and text modes but on many other systems (such as the ones on
- which Microsoft Word is most commonly found) there is a difference.
-
- --
- -----------------------------------------
- Lawrence Kirby | fred@genesis.demon.co.uk
- Wilts, England | 70734.126@compuserve.com
- -----------------------------------------
-